<template>
  <div class="container">
    <div class="header-content">
      <h3 v-if="managerType == '超级管理员'">新增一级管理员</h3>
      <h3 v-if="managerType == '一级管理员'">
        新增{{ university.schoolName }}管理员
      </h3>
    </div>
    <div class="my-content">
      <div class="my-content-item">
        <el-form label-position="right" label-width="80px" :model="formManager">
          <el-form-item label="学校">
            <template v-if="managerType == '超级管理员'">
              <el-select
                v-model="formManager.universityId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in universityList"
                  :key="item.id"
                  :label="item.schoolName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
            <template v-else>
              <el-select
                v-model="formManager.universityId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  :label="university.schoolName"
                  :value="university.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="formManager.name"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="formManager.gender">
              <el-radio label="1">
                <li class="el-icon-male"></li>
                男
              </el-radio>
              <el-radio label="0"
                ><li class="el-icon-female"></li>
                女
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="formManager.phone"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="my-content-item">
        <el-button id="btn" type="primary" @click="addSubmit"
          >新增管理员</el-button
        >
      </div>
    </div>
  </div>
</template>
      </div>
    </div>
  </div>
</template>

<script>
import { getAllUniversity, addManager, getUniversityById } from "@/api";
import { validPhone } from "@/utils/validate";
export default {
  data() {
    return {
      managerType: "", //当前管理员类型
      formManager: {
        universityId: null,
        name: "",
        gender: "1", //0:女，1：男
        phone: "",
        type: 0, //管理员类型0：一级，1：二级，2：三级
      },
      universityList: [],
      university: {},
    };
  },
  created() {
    this.managerType = this.$store.state.user.userInfo.type;
    this.initUniversityList();
  },
  methods: {
    async addSubmit() {
      if (
        this.formManager.name === "" ||
        this.formManager.name === " " ||
        this.formManager.name == null
      ) {
        this.$message.warning("姓名不能为空");
        return;
      }
      let flag = validPhone(this.formManager.phone);
      if (flag) {
        //发送请求
        //如果当前管理员是超级管理员，可以新增一级管理员
        if (this.managerType == "超级管理员") {
          this.formManager.type = 1;
        } else if (this.managerType == "一级管理员") {
          this.formManager.type = 2;
        }
        //如果当前管理员是一级管理员，可以新增二级管理员
        let res = await addManager(this.formManager);
        if (res.code == 0) {
          this.$message.success("新增管理员成功");
          this.$router.push({ name: "manageManager" });
        } else {
          this.$message.error(res.msg);
        }
      } else {
        this.$message.warning("手机号格式错误");
      }
    },
    //获取所有大学列表
    async initUniversityList() {
      if (this.managerType == "一级管理员") {
        let res = await getUniversityById(
          this.$store.state.user.userInfo.universityId
        );
        if (res.code == 0) {
          this.university = res.data;
        } else {
          this.$message.error(res.msg);
        }
        return;
      }
      let res = await getAllUniversity();
      if (res.code == 0) {
        this.universityList = res.data;
      } else {
        this.$message.error("获取大学列表失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  background: #f0f2f5;
  padding: 10px;
  display: flex;
  align-items: center;
  flex-direction: column;
  .header-content {
    width: 70%;
    border-bottom: 1px solid rgb(217, 212, 212);
    text-align: center;
    border-radius: 10px 10px 0px 0px;
    background: rgb(255, 255, 255);
  }
  .my-content {
    height: 540px;
    padding-bottom: 20px;
    border-radius: 0px 0px 10px 10px;
    background: rgb(255, 255, 255);
    width: 70%;
    .my-content-item {
      margin-top: 10px;
      padding: 20px;
    }
  }
}
#btn {
  margin-left: 40px;
}
</style>